package com.example.service;

import com.example.dao.BlogDao;
import com.example.dto.BlogDto;
import com.example.entity.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author guochao
 * @version 1.0
 * @date 2021/12/13 16:48
 */
@Service
public class BlogService {
    @Resource
    private BlogDao blogDao;

    public void listPage(BlogDto blogDto) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "id");
        Sort sort = Sort.by(order);
        Pageable pageable = PageRequest.of(blogDto.getPageNumber(), blogDto.getPageSize(), sort);

        Specification<Blog> specification = (root, query, criteriaBuilder) -> null;
        Page<Blog> page = blogDao.findAll(specification, pageable);
        System.out.println(page.getTotalPages());
    }

    public void save(List<Blog> list){
        blogDao.saveAll(list);
    }
}
